/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.ce.commands.queries;

import com.bionic.ce.beans.Query;
import com.bionic.ce.businesslogic.QueryService;
import com.bionic.ce.commands.ICommand;
import com.bionic.ce.manager.Config;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author MAXIM
 */
public class CommandQueriesSearch implements ICommand {

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String page = null;
        Date finishDate = null;
        Double amount = null;
        String currencyFrom = null;
        String currencyTo = null;
        Double rateFrom = null;
        Double rateTo = null;
        try {
            if (!request.getParameter("amount").isEmpty()) {
                amount = Double.parseDouble(request.getParameter("amount"));
            }

            if (!request.getParameter("currencyFrom").isEmpty()) {
                currencyFrom = request.getParameter("currencyFrom");
            }
            if (!request.getParameter("currencyTo").isEmpty()) {
                currencyTo = request.getParameter("currencyTo");
            }
            if (!request.getParameter("currencyFrom").isEmpty()
                    & !request.getParameter("currencyTo").isEmpty()) {
                if (currencyFrom.equals(currencyTo)) {
                    throw new IllegalArgumentException("Currency from and Currency to can`t be the same");
                }
            }
            if (!request.getParameter("rateFrom").isEmpty()) {
                rateFrom = Double.parseDouble(request.getParameter("rateFrom"));
            }
            if (!request.getParameter("rateTo").isEmpty()) {
                rateTo = Double.parseDouble(request.getParameter("rateTo"));
            }
            if (!request.getParameter("rateFrom").isEmpty()
                    & !request.getParameter("rateTo").isEmpty()) {
                if (rateFrom > rateTo) {
                    throw new IllegalArgumentException("Rate from couldn`t be more than Rate to");
                }
            }

            if (!request.getParameter("finishDate").isEmpty()) {
                finishDate = new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("finishDate"));
                if (finishDate.before(new Date())) {
                    throw new IllegalArgumentException("Wrong value of finish date");
                }
            }
            Query query = new Query();
            query.setAmount(amount);
            query.setCurrencyFrom(currencyFrom);
            query.setCurrencyTo(currencyTo);
            query.setRateFrom(rateFrom);
            query.setRateTo(rateTo);
            query.setFinishDate(finishDate);
            query.setStatus("Active");
            System.out.println(query);

            List<Query> list = QueryService.findByDynamicSelect(query);
            request.setAttribute("searchedquerieslist", list);
            page = Config.getInstance().getProperty(Config.GOTOSEARCHEDQUERIES);
        } catch (ParseException ex) {
            request.setAttribute("searcherror", "Wrong value of finish date. Please provide correct value.");
            page = Config.getInstance().getProperty(Config.SEARCHQUERIES);
        } catch (NumberFormatException ex) {
            request.setAttribute("searcherror", "Please enter correct values to such fields: Amount, Rate from and Rate to");
            page = Config.getInstance().getProperty(Config.SEARCHQUERIES);
        } catch (IllegalArgumentException ex) {
            request.setAttribute("searcherror", ex.getMessage());
            page = Config.getInstance().getProperty(Config.SEARCHQUERIES);
        }
        (new CommandGoToQueries()).execute(request, response);
        return page;
    }
}
